import {
  FieldMetadataType,
  RelationOnDeleteAction,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildCompanyStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<AllStandardObjectFieldName<'company'>, FlatFieldMetadata> => ({
  // Base fields from BaseWorkspaceEntity
  id: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Company-specific fields
  name: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'name',
      type: FieldMetadataType.TEXT,
      label: 'Name',
      description: 'The company name',
      icon: 'IconBuildingSkyscraper',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  domainName: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'domainName',
      type: FieldMetadataType.LINKS,
      label: 'Domain Name',
      description:
        'The company website URL. We use this url to fetch the company icon',
      icon: 'IconLink',
      isNullable: true,
      isUnique: true,
      settings: {
        maxNumberOfValues: 1,
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  address: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'address',
      type: FieldMetadataType.ADDRESS,
      label: 'Address',
      description: 'Address of the company',
      icon: 'IconMap',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  addressOld: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'addressOld',
      type: FieldMetadataType.TEXT,
      label: 'Address (deprecated)',
      description:
        'Address of the company - deprecated in favor of new address field',
      icon: 'IconMap',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  employees: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'employees',
      type: FieldMetadataType.NUMBER,
      label: 'Employees',
      description: 'Number of employees in the company',
      icon: 'IconUsers',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  linkedinLink: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'linkedinLink',
      type: FieldMetadataType.LINKS,
      label: 'Linkedin',
      description: 'The company Linkedin account',
      icon: 'IconBrandLinkedin',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  xLink: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'xLink',
      type: FieldMetadataType.LINKS,
      label: 'X',
      description: 'The company Twitter/X account',
      icon: 'IconBrandX',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  annualRecurringRevenue: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'annualRecurringRevenue',
      type: FieldMetadataType.CURRENCY,
      label: 'ARR',
      description:
        'Annual Recurring Revenue: The actual or estimated annual revenue of the company',
      icon: 'IconMoneybag',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  idealCustomerProfile: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'idealCustomerProfile',
      type: FieldMetadataType.BOOLEAN,
      label: 'ICP',
      description:
        'Ideal Customer Profile: Indicates whether the company is the most suitable and valuable customer for you',
      icon: 'IconTarget',
      isNullable: false,
      defaultValue: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  position: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'position',
      type: FieldMetadataType.POSITION,
      label: 'Position',
      description: 'Company record position',
      icon: 'IconHierarchy2',
      isSystem: true,
      isNullable: false,
      defaultValue: 0,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdBy: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'createdBy',
      type: FieldMetadataType.ACTOR,
      label: 'Created by',
      description: 'The creator of the record',
      icon: 'IconCreativeCommonsSa',
      isUIReadOnly: true,
      isNullable: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  searchVector: createStandardFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'searchVector',
      type: FieldMetadataType.TS_VECTOR,
      label: 'Search vector',
      description: 'Field used for full-text search',
      icon: 'IconUser',
      isSystem: true,
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Relation fields
  people: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'people',
      label: 'People',
      description: 'People linked to the company.',
      icon: 'IconUsers',
      isNullable: true,
      createdAt,
      targetObjectName: 'person',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  accountOwner: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'accountOwner',
      label: 'Account Owner',
      description:
        'Your team member responsible for managing the company account',
      icon: 'IconUserCircle',
      isNullable: true,
      createdAt,
      targetObjectName: 'workspaceMember',
      targetFieldName: 'accountOwnerForCompanies',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'accountOwnerId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  taskTargets: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'taskTargets',
      label: 'Tasks',
      description: 'Tasks tied to the company',
      icon: 'IconCheckbox',
      isUIReadOnly: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'taskTarget',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  noteTargets: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'noteTargets',
      label: 'Notes',
      description: 'Notes tied to the company',
      icon: 'IconNotes',
      isUIReadOnly: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'noteTarget',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  opportunities: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'opportunities',
      label: 'Opportunities',
      description: 'Opportunities linked to the company.',
      icon: 'IconTargetArrow',
      isNullable: true,
      createdAt,
      targetObjectName: 'opportunity',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  favorites: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'favorites',
      label: 'Favorites',
      description: 'Favorites linked to the company',
      icon: 'IconHeart',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'favorite',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  attachments: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'attachments',
      label: 'Attachments',
      description: 'Attachments linked to the company',
      icon: 'IconFileImport',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'attachment',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  timelineActivities: createStandardRelationFieldFlatMetadata({
    objectName: 'company',
    workspaceId,
    options: {
      fieldName: 'timelineActivities',
      label: 'Timeline Activities',
      description: 'Timeline Activities linked to the company',
      icon: 'IconIconTimelineEvent',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'timelineActivity',
      targetFieldName: 'company',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
